package com.small.nacos.core.cluster;

import com.alibaba.nacos.common.utils.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author zhoujin
 * @Date 2022/11/10 18:52
 */
public abstract class Task implements Runnable {

    protected static final Logger LOGGER = LoggerFactory.getLogger(Task.class);

    protected volatile Boolean shutdown = false;

    @Override
    public void run() {
        if (!shutdown) {
            try {
                executeBody();
            } catch (Exception e) {
                LOGGER.error("this task execute has error : {}", ExceptionUtil.getStackTrace(e));
            } finally {
                after();
            }
        }
    }

    abstract void executeBody();

    protected void after() {

    }

    public void shutdown() {
        shutdown = true;
    }
}
